(function() {
  var regExp = config.regExp
  var $account = $('[name=username]')
  var $phone = $('[name=phone]')
  var $email = $('[name=email]')
  var $password = $('[name=password]')
  var $pwconfirm = $('[name=pwconfirm]')

  var KEY_ALL = 0
  var KEY_PHONE = 1
  var KEY_EMAIL = 2

  function showCheck($dom, checkInfo) {
    if(!checkInfo.result) {
      $dom.next('.help-block').show().text(checkInfo.text)
    }
    return checkInfo.result
  }

  function checkPhone() {
    var checkInfo = {
      result: true
    }
    if(!regExp.phone.test($phone.trimVal())) {
      checkInfo.result = false
      checkInfo.text = '手机号格式不正确'
    }

    if($phone.trimVal() === '') {
      checkInfo.result = false
      checkInfo.text = '请输入手机号'
    }
    return checkInfo
  }

  function checkAccount() {
    var checkInfo = {
      result: true
    }
    /*
    if(!regExp.account.test($account.trimVal())) {
      checkInfo.result = false
      checkInfo.text = '请输入2-20位用户名'
    }
    */

    if($account.trimVal() === '') {
      checkInfo.result = false
      checkInfo.text = '请输入用户名'
    }
    return checkInfo
  }

  function checkEmail() {
    var checkInfo = {
      result: true
    }
    if(!regExp.email.test($email.trimVal())) {
      checkInfo.result = false
      checkInfo.text = '邮箱格式不正确'
    }

    if($email.trimVal() === '') {
      checkInfo.result = false
      checkInfo.text = '请输入邮箱'
    }
    return checkInfo
  }

  function checkPassword() {
    var checkInfo = {
      result: true
    }
    if(!regExp.password.test($password.trimVal()) || $password.trimVal() === '') {
      checkInfo.result = false
      checkInfo.text = '请输入6-20位密码'
    }
    return checkInfo
  }

  function checkConfirm() {
    var checkInfo = {
      result: true
    }

    if(!checkPassword().result) {
      return checkInfo
    }

    if( !($('[name=password]').val() === $('[name=pwconfirm]').val()) ) {
      checkInfo.result = false
      checkInfo.text = '两次输入密码不一致'
    }
    return checkInfo
  }

  function checkExist(key,cb) {
    var data = {}
    switch (key) {
      case KEY_ALL:
        data.number = $phone.val()
        data.email = $email.val()
        break;
      case KEY_PHONE:
        data.number = $phone.val()
        break;
      case KEY_EMAIL:
        data.email = $email.val()
        break;
    }

    $.ajax({
      url: '/api/user/check',
      method: 'post',
      dataType: 'json',
      data: data,
      success: function (res) {
        if(res.data instanceof Array) {
          $.each(res.data, function (i, item) {
            var curItem = item
            if(item === 'number') {
              curItem = 'phone'
            }
            showCheck($('[name='+ curItem +']'), {
              result: false,
              text: '该'+ (item==='number'?'手机号':'邮箱') +'已存在'
            })
          })

          return
        }
        cb && cb()
      }
    })
  }

  $('.uk-form-row input').on('focus', function () {
    $(this).next('.help-block').hide()
  })

  $account.on('blur', function () {
    var accountCheckInfo = checkAccount()
    if(showCheck($(this),accountCheckInfo)) {

    }
  })

  $phone.on('blur', function () {
    var phoneCheckInfo = checkPhone()
    if(showCheck($(this),phoneCheckInfo)) {
      checkExist(KEY_PHONE)
    }
  })

  $email.on('blur', function () {
    var checkInfo = checkEmail()
    if(showCheck($(this),checkInfo)) {
      checkExist(KEY_EMAIL)
    }
  })

  $password.on('blur', function () {
    var checkInfo = checkPassword()
    if(showCheck($(this),checkInfo)) {

    }
  })

  $pwconfirm.on('blur', function () {
    var checkInfo = checkConfirm()
    if(showCheck($(this),checkInfo)) {

    }
  })

  $('#J_register').on('click', function () {
    if(!showCheck($account, checkAccount())) {
      return
    }

    if(!showCheck($phone, checkPhone())) {
      return
    }

    if(!showCheck($email, checkEmail())) {
      return
    }

    if(!showCheck($password, checkPassword())) {
      return
    }

    if(!showCheck($pwconfirm, checkConfirm())) {
      return
    }

    checkExist(KEY_ALL, function () {
      $.ajax({
        url: '/api/user/register',
        method: 'post',
        data: {
          nickname: $account.val(),
          number: $phone.val(),
          password: $password.val(),
          email: $email.val()
        },
        success: function (res) {
          if(res.data == 'true'){
            $('.uk-form-row').hide();
            $('.form-signin').hide();
            $('.header').show();
          }
          if(res.status.code == 0) {
            setTimeout(function () {
              openidlogin({
                account: $phone.val(),
                password: $password.val()
              })
            },1000);
          }
        }

      })
    })
  })


}());
